from selenium.webdriver.support import expected_conditions as EC
from longguo_ui.base.base import BasePage
from longguo_ui.config.locators import LoginPageLocators


class LoginPage(BasePage):
    def __init__(self, driver):
        super().__init__(driver)

    def enter_username(self, username):
        """输入用户名"""
        self.send_keys(LoginPageLocators.USERNAME_INPUT, username)

    def enter_password(self, password):
        """输入密码"""
        self.send_keys(LoginPageLocators.PASSWORD_INPUT, password)

    def click_login_button(self):
        """点击登录按钮"""
        self.click(LoginPageLocators.LOGIN_BUTTON)

    def login(self, username, password):
        """完整登录流程"""
        self.enter_username(username)
        self.enter_password(password)
        self.click_login_button()

    def is_login_failed(self):
        """判断登录是否失败（显示错误提示）"""
        try:
            self.wait.until(EC.presence_of_element_located(LoginPageLocators.LOGIN_ERROR))
            self.logger.info("登录失败，检测到错误提示")
            return True
        except:
            self.logger.info("登录成功，未检测到错误提示")
            return False

    def get_error_message(self):
        """获取登录错误信息"""
        return self.get_text(LoginPageLocators.LOGIN_ERROR)